package com.ecloud.scheduler.init;

import com.ecloud.scheduler.constant.CronEnum;
import com.ecloud.scheduler.job.DailyDataTransferJob;
import com.ecloud.scheduler.job.PresentDataTransferJob;
import com.ecloud.scheduler.trigger.CronTriggerFactory;
import lombok.RequiredArgsConstructor;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @author Squbi
 */
@Service
@RequiredArgsConstructor
public class SchedulingLoader {
    private final Scheduler scheduler;
    private final CronTriggerFactory triggerFactory;
    private final DailyDataTransferJob dataDailyJob;
    private final PresentDataTransferJob presentJob;

    @PostConstruct
    public void init() throws SchedulerException {
        initDaily();
        initPresent();
    }

    private void initDaily() throws SchedulerException {
        JobDetail jobDetail = dataDailyJob.toDetail();
        Trigger trigger = triggerFactory.create(CronEnum.EVERY_DAY_2_AM.getCron());
        scheduler.scheduleJob(jobDetail, trigger);
    }

    private void initPresent() throws SchedulerException {
        JobDetail jobDetail = presentJob.toDetail();
        Trigger trigger = triggerFactory.create(CronEnum.EVERY_10_MIN.getCron());
        scheduler.scheduleJob(jobDetail, trigger);
    }
}
